package services.common;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.h2.engine.User;

import models.common.bean.CurrUser;
import models.common.entity.t_batch_upload;
import services.base.BaseService;

import common.utils.Factory;
import common.utils.PageBean;
import common.utils.ResultInfo;

import daos.common.BatchUploadDao;

/**
 * 银行列表service实现
 * 
 * @description 
 *
 * @author ChenZhipeng
 * @createDate 2015年12月23日
 */
public class BatchUploadService extends BaseService<t_batch_upload> {

	private BatchUploadDao batchUploadDao = Factory.getDao(BatchUploadDao.class);
	
	protected BatchUploadService() {
		super.basedao = this.batchUploadDao;
	}

	/**
	 * 查询上传列表
	 * @description 
	 *
	 * @author Chenzhipeng
	 * @createDate 2015年12月25日
	 * @param userId
	 * @return
	 */
	public List<t_batch_upload> queryAllBankList() {
		
		return batchUploadDao.findListByColumn(" 1=1 ORDER BY id desc");
	}
	
	/**
	 * 
	 * @Title: pageOfBanks 
	 * @param  
	 * @return: PageBean<t_batch_upload>
	 * @author: liaijun   
	 * @date: 2017年10月17日 上午10:43:59 
	 * @version: V1.0
	 */
	public PageBean<t_batch_upload> pageOfBanks(int currPage, int pageSize,String bankName){
		if (bankName == null) {
			String sqlCount = "select count(1) from t_batch_upload";
			String sqlQuery = "select id,code,name,bank_no from  t_batch_upload";
			return batchUploadDao.pageOfBySQL(currPage, pageSize, sqlCount, sqlQuery, null);
		}else {
			String sqlCount = "select count(1) from t_batch_upload where name like :bankName";
			String sqlQuery = "select id,code,name,bank_no from  t_batch_upload where name like :bankName";
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("bankName", "%"+bankName+"%");
			return batchUploadDao.pageOfBySQL(currPage, pageSize, sqlCount, sqlQuery, map);
		}
		
	}
	

	
	/**
	 * 删除上传信息
	 * @Title: delete 
	 * @param  
	 * @return: int
	 * @author: liaijun   
	 * @date: 2017年10月17日 下午12:53:16 
	 * @version: V1.0
	 */
	public int delete(long id){
		String sql = "delete from  t_batch_upload where id = :id ";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		return batchUploadDao.executSQL(sql, map);
	}

	
	/**
	 * 批量上传文件记录保存
	 * @param fileName 文件名
	 * @param fileUrl 文件url 
	 * @param userId 上传用户id 
	 * @param type 类型：0 债权，1 债务
	 * @return
	 * @author LiangQiongfeng
	 *@version 2018-1-11 
	 */
	public int addButchFile(String fileName, String fileUrl, long userId, int type) {
		t_batch_upload batchUpload = new t_batch_upload();
		//判断文件名是否为空
		if(fileName != null && fileName.trim().length()>0){
			batchUpload.file_name = fileName;
		}
		//文件url判断
		if(fileUrl != null && fileUrl.trim().length()>0){
			batchUpload.file_url = fileUrl;
		}
		if(userId > 0 ){
			batchUpload.user_id = userId;
		}
		if(type ==0 || type == 1){
			batchUpload.type = type;
		}
		batchUpload.time = new Date();
		batchUpload.status = 0;
		boolean flag = batchUploadDao.save(batchUpload);
		if(flag){
			return 1;
		}
		return 0;
	}

	/**
	 *根据id删除文件
	 * @param id
	 * @param type
	 * @return
	 * @author LiangQiongfeng
	 *@version 2018-1-12 
	 */
	public int toDelete(long id, int type) {
		t_batch_upload batch_upload = batchUploadDao.findByID(id);
		int oldStatus = batch_upload.status;
		StringBuffer delSQL = new StringBuffer("UPDATE t_batch_upload SET status = -1 WHERE 1 = 1");
		Map<String,Object> condition = new HashMap<String, Object>();
		//改变其状态，以原状态作条件筛选，减少并发问题
		if(oldStatus >= 0 ){
			condition.put("oldStatus", oldStatus);
			delSQL.append(" AND status = :oldStatus");
		}
		if(id > 0){
			condition.put("id", id);
			delSQL.append(" AND id = :id");
		}
		
		return batchUploadDao.deleteBySQL(delSQL.toString(), condition);
	}


	/**
	 *	分页查询
	 * @param currPage
	 * @param pageSize
	 * @param type
	 * @return
	 * @author LiangQiongfeng
	 *@version 2018-1-12 
	 */
	public PageBean findByType(int currPage, int pageSize, int type,long user_id) {
		Map<String, Object> condition = new HashMap<String, Object>();
		StringBuffer querySQL = new StringBuffer("SELECT id,file_name,user_id,time,status,file_url,type FROM t_batch_upload WHERE 1 = 1 ");
		StringBuffer countSQL = new StringBuffer("SELECT count(1) FROM t_batch_upload WHERE 1 = 1 ");
		if(type != 0 && type != 1){
			return null;
		}
		if(user_id < 0){
			return null;
		}
		if (type >= 0) {
			querySQL.append(" AND type = :type");
			countSQL.append(" AND type = :type");
			condition.put("type", type);
		}
		if(user_id > 0){
			querySQL.append(" AND user_id = :user_id");
			countSQL.append(" AND user_id = :user_id");
			condition.put("user_id", user_id);
		}
		querySQL.append(" AND status NOT IN (1,-1)");
		countSQL.append(" AND status NOT IN (1,-1)");
		
		return batchUploadDao.pageOfBySQL(currPage, pageSize, countSQL.toString(), querySQL.toString(), condition);
	
	}

	/**
	 *	更新文件状态
	 * @param type 状态
	 * @param userId  
	 * @return
	 * @author LiangQiongfeng
	 *@version 2018-1-15 
	 * @param id 
	 */
	public int updateStatus(int type,long fileId, long userId) {
		
		StringBuffer updataSql = new StringBuffer("UPDATE t_batch_upload SET status = :status WHERE 1 = 1");
		Map<String, Object> condition = new HashMap<String, Object>();
		if(fileId > 0){
			updataSql.append(" AND id = :fileId");
			condition.put("fileId",fileId);
		}
		if (type >= 0){
			updataSql.append(" AND type = :type");
			condition.put("type",type);
		}
		if(userId > 0){
			updataSql.append(" AND user_id = :user_id");
			condition.put("user_id",userId);
		}
		updataSql.append(" AND status = :oldStatus");
		condition.put("oldStatus", t_batch_upload.UploadStatus.NO_PAY.code);
		
		condition.put("status",t_batch_upload.UploadStatus.UN_FINISHED.code);
		
		int count = batchUploadDao.updateBySQL(updataSql.toString(), condition);
		return count;
	}

	/**
	 *	获取当前用户上传的所有未申请文件
	 * @param id
	 * @param type
	 * @return
	 * @author LiangQiongfeng
	 *@version 2018-2-6 
	 */
	public List<t_batch_upload> findByUserId(long id, int type) {
		StringBuffer querySQL = new StringBuffer("SELECT id,file_name,user_id,time,status,file_url,type FROM t_batch_upload WHERE 1 = 1 ");
		
		Map<String,Object> condition = new HashMap<String,Object>();
		querySQL.append(" AND status = :status");
		condition.put("status", t_batch_upload.UploadStatus.NO_PAY.code);
		if(id > 0 && type != -1){
			querySQL.append(" AND user_id = :user_id");
			condition.put("user_id", id);
			querySQL.append(" AND type = :type");
			condition.put("type",type);
		}
		
		return batchUploadDao.findListBySQL(querySQL.toString(), condition);
	}


	
}
